package com.pinyoumei.app.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.FirstloadNewBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/7/31.
 */

public class GoodsIndexNewAdapter extends RecyclerView.Adapter<GoodsIndexNewAdapter.BaseViewHolder> {
    private List<FirstloadNewBean.FirstloadNewDto> listBeans = new ArrayList<>();
    private Context context;

    public GoodsIndexNewAdapter(Context context){
        this.context = context;
    }

    public void replaceAll(List<FirstloadNewBean.FirstloadNewDto> list) {
        listBeans.clear();
        if (list != null && list.size() > 0) {
            listBeans.addAll(list);
        }
        notifyDataSetChanged();
    }

    /**
     * 插入数据使用notifyItemInserted，如果要使用插入动画，必须使用notifyItemInserted
     * 才会有效果。即便不需要使用插入动画，也建议使用notifyItemInserted方式添加数据，
     * 不然容易出现闪动和间距错乱的问题
     * */
    public void addData(int position, List<FirstloadNewBean.FirstloadNewDto> list) {
        listBeans.addAll(position,list);
        notifyItemInserted(position);
    }

    //移除数据使用notifyItemRemoved
    public void removeData(int position) {
        listBeans.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OneViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_first_bottom, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) {
        baseViewHolder.setData(listBeans.get(i),i);
        if(null != mOnItemClickLitener){
            baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(null,i);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return listBeans != null ? listBeans.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(FirstloadNewBean.FirstloadNewDto data, int position) {

        }
    }

    private class OneViewHolder extends BaseViewHolder {
        private ImageView ivImage;
        private LinearLayout llLayout,tvSalePriceLayout,tvSaleIconLayout;
        private TextView ifbTag1,ifbTag2,tvGoodsName,tvSalePrice,tvMarketPrice;

        public OneViewHolder(View view) {
            super(view);
            ivImage = (ImageView) view.findViewById(R.id.iv_bg);
            llLayout = (LinearLayout) view.findViewById(R.id.ll_layout);
            tvSalePriceLayout = (LinearLayout) view.findViewById(R.id.tv_sale_price_layout);
            tvSaleIconLayout = (LinearLayout) view.findViewById(R.id.tv_sale_icon_layout);
            ifbTag1 = (TextView) view.findViewById(R.id.ifb_tag1);
            ifbTag2 = (TextView) view.findViewById(R.id.ifb_tag2);
            tvGoodsName = (TextView) view.findViewById(R.id.tv_goods_name);
            tvSalePrice = (TextView) view.findViewById(R.id.tv_sale_price);
            tvMarketPrice = (TextView) view.findViewById(R.id.tv_market_price);
        }

        @Override
        void setData(FirstloadNewBean.FirstloadNewDto data, int position) {
            if (data != null) {
                if(TextUtils.isEmpty(data.getGoods_img())){
                    llLayout.setBackgroundResource(R.drawable.shanpe_f5f5f5_10dp);
                    ivImage.setImageResource(R.mipmap.luck_draw_icon);
                }else {
                    Glide.with(context).load(data.getGoods_img()).into(ivImage);
                    llLayout.setBackgroundResource(R.drawable.shanpe_white_10dp);
                }

                if(position == 0){
                    tvGoodsName.setVisibility(View.GONE);
                    tvSalePriceLayout.setVisibility(View.GONE);
                    tvSaleIconLayout.setVisibility(View.GONE);
                }else{
                    tvGoodsName.setVisibility(View.VISIBLE);
                    tvSalePriceLayout.setVisibility(View.VISIBLE);
                    tvSaleIconLayout.setVisibility(View.VISIBLE);
                }

                ifbTag1.setText(data.getTag1());
                ifbTag2.setText(data.getTag2());
                tvGoodsName.setText(data.getGoods_name());
                tvSalePrice.setText(data.getSale_price());
                tvMarketPrice.setText("¥"+data.getMarket_price());
                tvMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                //需要Item高度不同才能出现瀑布流的效果，此处简单粗暴地设置一下高度
                if (position % 2 == 0) {
                    if(position == 0){
                        ivImage.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 620));
                    }else {
                        ivImage.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 490));
                    }
                } else {
                    ivImage.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 490));
                }

                if(TextUtils.isEmpty(data.getTag1())){
                    ifbTag1.setVisibility(View.INVISIBLE);
                }else{
                    ifbTag1.setVisibility(View.VISIBLE);
                }
                if(TextUtils.isEmpty(data.getTag2())){
                    ifbTag2.setVisibility(View.INVISIBLE);
                }else{
                    ifbTag2.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

}

